package org.mspring.mlog.service.impl;

import java.util.Date;
import java.util.List;

import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;
import org.mspring.mlog.entity.Comment;
import org.mspring.mlog.service.CommentService;
import org.mspring.mlog.utils.CommentCacheUtils;
import org.mspring.platform.dao.hibernate.GenericDao;
import org.mspring.platform.dao.hibernate.common.EnhancedRule;
import org.mspring.platform.dao.support.Page;
import org.mspring.platform.dao.support.Sort;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * 
 * @author Gao Youbo
 * @since 2013-06-18 16:34:42
 */
@Service
@Transactional
public class CommentServiceImpl extends GenericDao<Comment, java.lang.Long> implements CommentService {

    @Override
    public void approved(Long... ids) {
        // TODO Auto-generated method stub
        for (Long id : ids) {
            updateCommentStatus(id, Comment.Status.APPROVED);
        }
    }

    @Override
    public void spam(Long... ids) {
        // TODO Auto-generated method stub
        // for (Long id : ids) {
        // updateCommentStatus(id, Comment.Status.SPAM);
        //
        // // 标记垃圾评论，提交垃圾评论规则
        // try {
        // String akismet_key = optionService.getOption("akismet_key");
        // if (StringUtils.isNotBlank(akismet_key)) {
        // Akismet akismet = new Akismet(akismet_key,
        // optionService.getOption("blogurl"));
        // Comment comment = get(id);
        // akismet.submitSpam(comment.getPostIp(), comment.getAgent(), null,
        // null, Akismet.COMMENT_TYPE_COMMENT, comment.getAuthor(),
        // comment.getEmail(), comment.getUrl(), comment.getContent(), null);
        // }
        // } catch (Exception e) {
        // // TODO: handle exception
        // continue;
        // }
        // }
    }

    @Override
    public void recycle(Long... ids) {
        // TODO Auto-generated method stub
        for (Long id : ids) {
            updateCommentStatus(id, Comment.Status.RECYCLE);
        }
    }

    @Override
    public void updateCommentStatus(Long id, String status) {
        // TODO Auto-generated method stub
        String queryString = "update Comment comment set comment.status = ? where comment.id = ?";
        executeUpdate(queryString, new Object[] { status, id });
    }

    @Override
    public Comment save(Comment object) {
        // TODO Auto-generated method stub
        if (object.getCreateTime() == null) {
            object.setCreateTime(new Date());
        }

        // 清理评论缓存
        CommentCacheUtils.clearCachedCommentList(object.getFkey(), object.getFtype());
        return super.save(object);
    }

    @Override
    public List<Comment> getRecentComment(int num) {
        // TODO Auto-generated method stub
        EnhancedRule rule = new EnhancedRule();
        rule.addOrder(Order.desc("createTime"));
        rule.setOffset(0);
        rule.setPageSize(num);
        rule.add(Restrictions.eq("status", Comment.Status.APPROVED));
        return list(rule);
    }

    @Override
    public List<Comment> getRecentComment(String fType, int num) {
        // TODO Auto-generated method stub
        EnhancedRule rule = new EnhancedRule();
        rule.addOrder(Order.desc("createTime"));
        rule.setOffset(0);
        rule.setPageSize(num);
        rule.add(Restrictions.eq("status", Comment.Status.APPROVED));
        rule.add(Restrictions.eq("fType", fType));
        return list(rule);
    }

    @Override
    public List<Comment> findComment(String fType) {
        // TODO Auto-generated method stub
        EnhancedRule rule = new EnhancedRule();
        rule.addOrder(Order.desc("createTime"));
        rule.setOffset(0);
        rule.add(Restrictions.eq("status", Comment.Status.APPROVED));
        rule.add(Restrictions.eq("fType", fType));
        return list(rule);
    }

    @Override
    public Page<Comment> findComment(String fType, Page<Comment> page) {
        // TODO Auto-generated method stub
        return findComment(fType, Comment.Status.APPROVED, page);
    }

    @Override
    public Page<Comment> findComment(String fType, String status, Page<Comment> page) {
        // TODO Auto-generated method stub
        String hql = "select comment from Comment comment where comment.fType = ? and comment.status = ?";
        return listPage(hql, page, fType, status);
    }

    @Override
    public Page<Comment> findComment(String ftype, String fkey, int page) {
        // TODO Auto-generated method stub
        return findComment(ftype, fkey, page, Comment.Status.APPROVED);
    }

    @Override
    public Page<Comment> findComment(String ftype, String fkey, int page, String status) {
        // TODO Auto-generated method stub
        Page<Comment> commentPage = new Page<Comment>();
        commentPage.setSort(new Sort("createTime", Sort.DESC));
        commentPage.setPageNo(page);
        return listPage("select comment from Comment comment where comment.ftype = ? and comment.fkey = ? and comment.status = ?", commentPage, ftype, fkey, status);
    }

    @Override
    public void safeRemove(Long id, Long user) {
        // TODO Auto-generated method stub
        executeUpdate("delete from Comment c where c.id = ? and c.toUser.id = ?", id, user);
    }

}
